package com.crocodileQ.blog.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.crocodileQ.blog.domain.BlogArticles;
import com.crocodileQ.blog.domain.vo.BlogCommentsVo;
import com.crocodileQ.blog.domain.vo.BlogUserVo;
import com.crocodileQ.common.constant.BlogConstants;
import com.crocodileQ.common.exception.base.BaseException;
import com.crocodileQ.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.crocodileQ.blog.mapper.BlogCommentsMapper;
import com.crocodileQ.blog.domain.BlogComments;
import com.crocodileQ.blog.service.IBlogCommentsService;

/**
 * 评论管理Service业务层处理
 * 
 * @author crocodilePole
 * @date 2025-04-16
 */
@Slf4j
@Service
public class BlogCommentsServiceImpl implements IBlogCommentsService 
{
    @Autowired
    private BlogCommentsMapper blogCommentsMapper;

    @Autowired
    private BlogArticlesServiceImpl blogArticlesService;

    @Autowired
    private BlogUserServiceImpl blogUserService;

    /**
     * 查询评论管理
     * 
     * @param id 评论管理主键
     * @return 评论管理
     */
    @Override
    public BlogComments selectBlogCommentsById(Long id)
    {
        return blogCommentsMapper.selectBlogCommentsById(id);
    }

    /**
     * 查询评论管理列表
     * 
     * @param blogComments 评论管理
     * @return 评论管理
     */
    @Override
    public List<BlogCommentsVo> selectBlogCommentsList(BlogComments blogComments)
    {
        List<BlogCommentsVo> blogCommentsVos = blogCommentsMapper.selectBlogCommentsList(blogComments);
        blogCommentsVos = blogCommentsVos.stream().peek(blogCommentsVo -> {
//            文章名称
            BlogArticles blogArticles = blogArticlesService.selectBlogArticlesById(blogCommentsVo.getArticleId());
            blogCommentsVo.setTitle(blogArticles.getTitle());
            Long userId = blogCommentsVo.getUserId();

            if (userId != null) {
                BlogUserVo blogUserVo = blogUserService.selectBlogUserById(userId);
                blogCommentsVo.setNickName(blogUserVo.getNickName());
            }
        }).collect(Collectors.toList());


        return blogCommentsVos;
    }

    /**
     * 新增评论管理
     * 
     * @param blogComments 评论管理
     * @return 结果
     */
    @Override
    public int insertBlogComments(BlogComments blogComments)
    {
        blogComments.setCreateTime(DateUtils.getNowDate());

        Long parentId = blogComments.getParentId();
        //1、rootId 为0，则为一级评论
        if(parentId == null){
            blogComments.setRootId(BlogConstants.BLOG_COMMENT_ROOT_ID);
            blogComments.setParentId(BlogConstants.BLOG_COMMENT_ROOT_ID);
            blogComments.setParentUserId(BlogConstants.BLOG_COMMENT_ROOT_ID);
            return blogCommentsMapper.insertBlogComments(blogComments);
        }
        //2、不为空，遍历查询根评论
        BlogComments dbComments = blogCommentsMapper.selectBlogCommentsById(parentId);
        //2.1、根评论不存在，抛出异常
        if(dbComments == null){
            throw new BaseException("根评论不存在");
        }
        //2.2、父评论存在，则获取并设置根评论id
        //如果父评论是一级评论，则根评论id为父评论id
        if(dbComments.getRootId() == null){
            blogComments.setRootId(dbComments.getId());
            blogComments.setParentId(dbComments.getId());
        }else{
            blogComments.setRootId(dbComments.getRootId());
            blogComments.setParentId(dbComments.getParentId());
        }

        blogComments.setParentUserId(dbComments.getUserId());
        return blogCommentsMapper.insertBlogComments(blogComments);

    }

    /**
     * 修改评论管理
     * 
     * @param blogComments 评论管理
     * @return 结果
     */
    @Override
    public int updateBlogComments(BlogComments blogComments)
    {
        blogComments.setUpdateTime(DateUtils.getNowDate());
        return blogCommentsMapper.updateBlogComments(blogComments);
    }

    /**
     * 批量删除评论管理
     * 
     * @param ids 需要删除的评论管理主键
     * @return 结果
     */
    @Override
    public int deleteBlogCommentsByIds(Long[] ids)
    {

        //1、查询子评论
        int count = blogCommentsMapper.countComments(ids);

        //2、不能删除，存在子评论
        if(count > 0){
            throw new BaseException("存在子评论，不能删除");
        }
        //3、删除，不存在子评论
        return blogCommentsMapper.deleteBlogCommentsByIds(ids);
    }

    /**
     * 删除评论管理信息
     * 
     * @param id 评论管理主键
     * @return 结果
     */
    @Override
    public int deleteBlogCommentsById(Long id)
    {
        return blogCommentsMapper.deleteBlogCommentsById(id);
    }

    /**
     * 根据文章id查询
     * @param id
     * @return integer
     **/
    public Integer countByArticleId(Long id) {

        return blogCommentsMapper.countByArticleId(id);
    }
}
